package co.gov.mintic.Biblioteca.dataSource;

import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import co.gov.mintic.Biblioteca.beans.Registro;
import co.gov.mintic.Biblioteca.vo.ConsultarLibro;

/**
 * Datasource
 * 
 * @author
 * 
 */
public class Datasource {
	// Singleton pattern
	private static Datasource datasource = null;
	private static int SIZE = 0;
	private static List<Registro> data = null;

	/**
	 * @return the data
	 */
	public static List<Registro> getData() {
		return data;
	}

	/**
	 * 
	 * @param valueToSearch
	 * @param busqueda
	 * @return
	 * @throws XmlPullParserException
	 */
	public static Datasource getInstance(String valueToSearch, String busqueda)
			throws XmlPullParserException {
		data = ConsultarLibro
				.getLibrosBySearchCriteria(valueToSearch, busqueda);
		if (datasource == null) {
			datasource = new Datasource(data);
		}
		return datasource;
	}

	@SuppressWarnings("static-access")
	/**
	 * 
	 * @param datos
	 */
	public Datasource(List<Registro> datos) {
		this.data = datos;
	}

	/**
	 * 
	 * @return
	 */
	public int getSize() {
		SIZE = data.size();
		return SIZE;
	}

	/**
	 * Returns the elements in a <b>NEW</b> list.
	 */
	public List<Registro> getData(int offset, int limit) {
		List<Registro> newList = new ArrayList<Registro>(limit);
		int end = offset + limit;
		if (end > data.size()) {
			end = data.size();
		}
		newList.addAll(data.subList(offset, end));
		return newList;
	}

}
